package com.artnology.independence.model;

import java.awt.Color;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RoundRectangle2D;

import org.eclipse.jdt.core.IClassFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.ITypeRoot;
import org.eclipse.jdt.core.JavaModelException;

import com.artnology.independence.Activator;
import com.artnology.independence.views.DependencyView;

public class DVType extends DVNode {

	private String packageName = null;
	private boolean isInterfaceType = false;
	


	public DVType(String simpleName, String packageName, DVGraphModel model) {
		super(simpleName, model);
		this.packageName = packageName;
	}
	
	public boolean isPhantom() {
		return getGraphModel().getAllPhantomTypes().contains(this);
	}


	@Override
	public Shape getPaintShape(double width, double height) {
		double x = (-width / 2) * getDrawingScalingFactor();
		double y = (-height / 2) * getDrawingScalingFactor();
		double w = width * getDrawingScalingFactor();
		double h = height * getDrawingScalingFactor();
		if (isInterfaceType()) {
			return new Ellipse2D.Double(x - 15, y - 2, w + 30, h + 3);
		} else {
			return new RoundRectangle2D.Double(x - 15, y - 2, w + 30, h + 3,
					10, 10);
		}
	}
	
	public static final DVType getDVTypeForJDTType(ITypeRoot iTypeRoot) {
		return (DVType) Activator
					.getDefault()
					.getModel()
					.getTypeForFQName(getFQNameFromJDTType(iTypeRoot));
	}
	
	private static final String getFQNameFromJDTType(ITypeRoot theJDTType) {
		String fqName = null;
		if (theJDTType instanceof ICompilationUnit) {
			ICompilationUnit compUnit = (ICompilationUnit) theJDTType;
			try {
				fqName = compUnit.getPackageDeclarations()[0]
								.getElementName().toString()+ "." + getClassnameFromFileName(compUnit.getElementName().toString());
			} catch (JavaModelException e) {
				e.printStackTrace();
			}
		} else {
			IClassFile classFile = (IClassFile) theJDTType;
			fqName = classFile.getType()
					.getPackageFragment().getElementName() + "." + classFile.getType()
			.getTypeQualifiedName();
		}
		return fqName;
	}
	
	public static final String getClassnameFromFileName(String filename) {
		return filename.substring(0, filename.indexOf('.'));
	}

	public boolean isInterfaceType() {
		return isInterfaceType;
	}

	public void setInterfaceType(boolean isInterfaceType) {
		this.isInterfaceType = isInterfaceType;
	}
	
	




	public String getFullyQualifiedName() {
		return getPackageName() + "." + getName();
	}

	public String getPackageName() {
		if (packageName == null) {
			packageName = DVPackage.DEFAULT_PACKAGE_NAME;
		}
		return packageName;
	}

	public DVPackage getPackage() {
		return getGraphModel().getPackage(getPackageName());
	}

	public String getLabel() {
		return getName();
	}

	@Override
	public Color getNodeFillColor() {

		if (isCurrentlySelected()) {
			return new Color(0, 0, 170);
		}

		if (Activator.getDefault().getPrefStore()
				.getBoolean(DependencyView.SHOW_COUPLING)) {
			return getCouplingColorCode();
		}
	
		if (isPhantom()) {
			return Color.WHITE;
		}
	
		return getPackage().getNodeFillColor();

	}

	@Override
	public boolean equals(Object arg0) {
		if (!(arg0 instanceof DVType)) {
			return false;
		} else {
			DVType mo = (DVType) arg0;
			return mo.getFullyQualifiedName().equals(getFullyQualifiedName());
		}
	}

	@Override
	public int hashCode() {
		return getFullyQualifiedName().hashCode();
	}

}
